<?php

namespace Tasker\Models\Calendar;

class Month extends \Nette\Object
{

	public $monthNumber = 0;
	public $weeks = array();
	private $startDate;
	private $numberOfWeeks;

	public function __construct($startDate, $events)
	{
		$this->startDate = $startDate;
		$this->monthNumber = date('n', strtotime('+7days', $startDate));
		$tmpDate = strtotime(date('Y-m-1', strtotime('+7days', $startDate)));
		$startWeek = date('W', $tmpDate);
		$endWeek = date('W', strtotime('+' . date('t', $tmpDate) . 'days', $tmpDate));
		if($endWeek > $startWeek)
			$this->numberOfWeeks = $endWeek - $startWeek + 1;
		else
			$this->numberOfWeeks = $endWeek - ( $startWeek-max(date('W', strtotime( date('Y-12-25',$startDate) ) ), date('W', strtotime( date('Y-12-31',$startDate) ) ) ) )  + 1;
		
		for ($i = 0; $i < $this->numberOfWeeks; $i++)
		{
			$weekDate = strtotime('+' . $i . 'week', $startDate);
			$weekEvents = array();
			for ($j = 0; $j < 7; $j++)
			{
				$dayStart = strtotime('+' . $j . 'days', $weekDate);
				if(isset($events[$dayStart])){
					$weekEvents[$dayStart] = $events[$dayStart];
				}
			}
			$this->weeks[$i] = new Week($weekDate, $weekEvents);
		}
	}

}

?>
